<template>
  <table-block>
    <template-query
      slot="query"
      :configs="queryUserFields"
      inline-item
      @query="handleSearch"
    >
    </template-query>
    <template-table
      :datas="datas"
      is-order-number
      :loading="isLoading"
      :configs="userListFields"
      need-mark-prop="status"
      :need-mark-prop-val="-1"
      @refresh="fetchData"
    >
      <el-table-column fixed="right" label="操作" width="135">
        <template slot-scope="{ row }">
          <popconfirm
            class="mr-1"
            title="删除"
            text="确认删除该用户"
            @confirm="handleDelete(row)"
          />
          <popconfirm
            v-if="!row.is_svip"
            title="设为svip"
            text="是否设为svip?"
            @confirm="handleShelvesSvip(row, 1)"
          />
          <popconfirm
            v-if="row.is_svip === 1"
            title="取消svip"
            text="是否取消svip?"
            @confirm="handleShelvesSvip(row, 0)"
          />
          <popconfirm
            v-if="row.status === 0"
            title="禁用"
            text="是否禁用?"
            @confirm="disableUser(row)"
          />
        </template>
      </el-table-column>
    </template-table>
    <el-pagination
      slot="pagination"
      :current-page="page"
      :page-size="limit"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleLimitChange"
    />
  </table-block>
</template>

<script>
import { getUserList } from 'dependencies/api/user'
import { userListFields, queryUserFields, columns } from './config'
import TablePageMixins from 'modules/mixins/table-page'
export default {
  name: 'SdkActivityList',
  mixins: [TablePageMixins],
  data() {
    return {
      userListFields,
      queryUserFields,
      queryParam: {}
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    getDatas() {
      return getUserList({
        formData: this.queryParam,
        pageIndex: this.page,
        pageSize: this.limit,
        columns: columns
      })
    },
    handleSearch(params) {
      this.queryParam = { ...params }
      this.page = 1
      this.fetchData()
    },
    handleShelvesSvip(row, isSvip) {
      //
      vk.callFunction({
      	url: 'admin/system/user/sys/updateSvip',
      	title: '请求中...',
      	data: {
          _id: row._id,
      		isSvip
      	},
      	success: (data) => {
      		this.$message.success('操作成功!')
          this.fetchData()
      	}
      });
    },
    handleDelete(row) {
      vk.callFunction({
      	url: 'admin/system/user/sys/deleteUser',
      	title: '请求中...',
      	data: {
      		_id: row._id
      	},
      	success: (data) => {
          this.fetchData()
          this.$message.success('操作成功!')
      	}
      });
    },
    disableUser(row) {
      vk.callFunction({
      	url: 'admin/system/user/sys/updateUserStatus',
      	title: '请求中...',
      	data: {
      		_id: row._id,
          status: 1
      	},
      	success: (data) => {
          this.fetchData()
          this.$message.success('操作成功!')
      	}
      });
    }
  }
}
</script>
